package com.lanyou.esb.web.serviceTesting;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.repository.FieldSystemRepository;
import com.lanyou.cook.repository.SystemInterfaceRepository;
import com.lanyou.cook.service.UserLogService;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.common.CommonUtils;
import com.lanyou.esb.datadictionary.EnableType;
import com.lanyou.esb.datadictionary.IfOperationType;
import com.lanyou.esb.datadictionary.IfReqType;
import com.lanyou.esb.entity.Http;
import com.lanyou.esb.repository.HttpRepository;
import com.lanyou.esb.service.HttpService;
import com.lanyou.esb.service.ServiceTestingService;
import com.lanyou.esb.service.ServiceUsabilityService;
import com.lanyou.esb.vo.HttpVo;
import com.lanyou.esb.web.param.ServiceTestingParam;

/**
 * 服务发布方测试
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/serviceProviderTesting")
public class ServiceProviderTestingController {
	@Autowired
	ServiceUsabilityService serviceUsabilityService;
	@Autowired
	HttpService httpInterfaceService;
	@Autowired
	ServiceTestingService serviceTestingService;
	@Autowired
	UserSessionService userSessionService;
	@PersistenceContext
	private EntityManager entityManager;
	@Autowired
	private SystemInterfaceRepository systemInterfaceRepository;

	@Autowired
	private FieldSystemRepository fieldSystemRepository;
	@Autowired
	private UserLogService userLogService;

	@Autowired
	HttpRepository httpRepository;

	/**
	 * 获取HTTP接口测试信息
	 * 
	 * @param interfaceId
	 *            ：接口ID
	 * @return
	 */
	@RequestMapping(value = "/getHttpInterfaceInfos", produces = "application/json")
	public String getHttpInterfaceInfos(String interfaceId) {
		Http http = httpInterfaceService.getHttp(interfaceId,
				"",
				EnableType.ENABLE.ValueCode);
		if (http == null) {
			return "{}";
		}
		String requestMess = http.getRequestMess();
		HttpVo httpVo = new HttpVo();
		httpVo.setId(http.getId());
		httpVo.setCode(http.getCode());
		httpVo.setName(http.getName());
		httpVo.setPriUrl(http.getPriUrl());
		httpVo.setType(http.getType());
		httpVo.setRequest(http.getRequest());
		httpVo.setDataFormate(http.getDataFormate());
		httpVo.setRequestMess(requestMess);
		httpVo.setContentType(http.getContentType());
		httpVo.setSystemCode(http.getSystemCode());
		httpVo.setVersion(http.getVersion());
		httpVo.setOperationType(http.getOperationType());
		httpVo.setSoapActionUri(http.getSoapActionUri());

		return GsonUtil.getDefaultGson().toJson(httpVo);
	}

	/**
	 * 服务测试请求发送
	 * 
	 * @param param
	 *            ：查询参数
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@RequestMapping(value = "/serviceTesting", produces = "application/json", method = RequestMethod.POST)
	public String serviceTesting(@RequestBody ServiceTestingParam param)
			throws HttpException, IOException, URISyntaxException {
		// TODO test
		// serviceUsabilityService.serviceUsabilityTest();
		if (StringUtils.isBlank(param.getRequest())) {
			param.setRequest(IfReqType.GET.ValueCode);
		}
		// 只能测试查找类型的接口
		if (StringUtils.isBlank(param.getOperationType())
				|| !IfOperationType.QUERY.ValueCode.equals(param
						.getOperationType())) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommonUtils.MSG_FIELD_RESULT, "只能测试查找类型的接口");
			return GsonUtil.getDefaultGson().toJson(result);
		}
		Map<String, Object> result = serviceTestingService.sendRequest(param,
				true);
		if (result == null) {
			return "{}";
		}
		return GsonUtil.getDefaultGson().toJson(result);
	}

}
